class Solution
{
public:
    Solution() {}
    int maxProductDifference(vector<int> &nums)
    {
        // 都是正数就比较简单了。如果有负数会复杂一些。
        int firstMin, secondMin, firstMax, secondMax;
        if (nums[0] > nums[1])
        {
            firstMin = secondMax = nums[1];
            firstMax = secondMin = nums[0];
        }
        else
        {
            firstMin = secondMax = nums[0];
            firstMax = secondMin = nums[1];
        }
        for (int i = 2; i < nums.size(); ++i)
        {
            if (nums[i] >= firstMax)
            {
                secondMax = firstMax;
                firstMax = nums[i];
            }
            else if (nums[i] >= secondMax)
            {
                secondMax = nums[i];
            }
            if (nums[i] <= firstMin)
            {
                secondMin = firstMin;
                firstMin = nums[i];
            }
            else if (nums[i] <= secondMin)
            {
                secondMin = nums[i];
            }
        }
        return firstMax * secondMax - firstMin * secondMin;
    }
};